<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use app\user\model\CommentModel;
use cmf\controller\UserBaseController;
use app\user\model\UserModel;
use think\Db;

class CommentController extends UserBaseController
{
    /**
     * 个人中心我的评论列表
     */
    public function index()
    {
        $user = cmf_get_current_user();

        $commentModel = new CommentModel();
        $comments     = $commentModel->where(['user_id' => cmf_get_current_user_id(), 'delete_time' => 0])
            ->order('create_time DESC')->paginate();
        $this->assign($user);
        $this->assign("page", $comments->render());
        $this->assign("comments", $comments);
        return $this->fetch();
    }

    /**
     * 用户删除评论
     */
    public function delete()
    {
        $id   = $this->request->param("id", 0, "intval");
        $delete = new UserModel();
        $data = $delete->deleteComment($id);
        if ($data) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     *   @Notes:用来添加文章下面用户的评论的
     *   @Function add
     *   @author: xy
     *   @Time: 2018/11/15 0015   13:23
     * xxx
     */
    public function add(){
        $data = $this->request->param();
        $parent_id = empty($data['parent_id'])?'':$data['parent_id'];//被回复的评论id
        $user_id = empty($data['user_id'])?'':$data['user_id'];//发表评论的用户id
        $to_user_id = empty($data['to_user_id'])?'':$data['to_user_id']; //被评论的用户id
        $object_id = empty($data['object_id'])?'':$data['object_id'];//被评论的文章 id
        $create_time = date("Y/m/d H:i:s");//评论时间
        $full_name = empty($data['user_login'])?'':$data['user_login'];//评论者昵称
        $email = empty($data['email'])?'':$data['email'];//评论者邮箱
        $content = empty($data['content'])?'':$data['content'];//评论内容
        $city = empty($data['city'])?'':$data['city'];//地址城市
        $url = empty($data['url'])?'':$data['url']; //评论文章所在的路径
        //$url = sp_get_relative_url($url);//去掉url 中的带协议的域名
        $type = empty($data['type'])?0:$data['type'];//评论类型；1评论回复；0文章评论
        $to_user_name = empty($data['to_user_name'])?'匿名':$data['to_user_name'];//被回复的用户人的昵称
        if(empty($content)){
            $this->error('我什么都没读到呀！');
        }
        if(mb_strlen($content,"utf-8")>=360){
            $this->error('请不要输入过长的话，比如小于360个字符？');
        }
        if($full_name ==''){
            $this->error('请联系网站管理员，有bug出现了！');
        }
        if($type == '1'){
            $floor = empty($data['floor'])?'':$data['floor']; //回复所在的楼层
            $to_user_name = Db::name('user')->where('id',$to_user_id)->find();
            if(!$to_user_name){
                $this->error('该评论人已经离开了本网站！');
            }else{
                $to_user_name = $to_user_name['user_login'];
            }
            if($user_id == $to_user_id){
                $this->error('回复自己的评论是不行的！');
            }
            $reple_data = [
                'parent_id' => $parent_id,
                'user_id'=>$user_id,
                'to_user_id' => $to_user_id,
                'object_id' => $object_id,
                'create_time' => $create_time,
                'full_name' => $full_name,
                'content' => $content,
                'url' => $url,
                'type' => $type,
                'to_user_name' =>$to_user_name,
                'floor' => $floor,
            ];
            $ret = Db::name('comment')->insert($reple_data);
            if($ret){
                $this->success('回复添加成功！!');
            }else{
                $this->error('服务器可能在发呆！请重新尝试下！');
            }
            $this->success('到达了!',null,$url);
        }
        if (!empty($user_id) && !empty($_SESSION['think']['token'])){
            $user_avatar = Db::name('user')->where('id',$user_id)->find();
            $floor = Db::name('portal_post')->where('id',$object_id)->find();
            $floor = $floor["reply_num"] +1;
            // 启动事务
            Db::startTrans();
            try{
                Db::name('portal_post')->where('id', $object_id)->update(['reply_num' => $floor]);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('服务器好像很忙的样子，麻烦稍后再试！');
            }

            $comm_data = [
                'user_id'=>$user_id,
                'object_id'=>$object_id,
                'create_time'=>$create_time,
                'full_name'=>$full_name,
                'email'=>$email,
                'content'=>$content,
                'avatar' =>$user_avatar['avatar'],
                'city' =>$city,
                'sex' => $user_avatar['sex'], //性别;0:保密,1:男,2:女
                'floor' => $floor,
                'url' =>$url,
                ];
            $ret = Db::name('comment')->insert($comm_data);
            if($ret){
                $this->success('添加成功',null,"成功添加：".$ret."条");
            }else{
                $this->error('写入失败！');
            }
        }else{
            $this->error('请先登录！', url('user/Login/index'));
        }
       // $this->success('测试回复',null,$type);

    }

    /**
     *   @Notes:用于展示评论
     *   @Function reply
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *   @author: xy
     *   @Time: 2018/11/19 0019   10:31
     * xxx
     */
    public function reply(){
        $data = $this->request->param();
        //$text = empty($data['object_id'])? '是空的！': $data['object_id'];
        $ret = Db::name('comment')->where('object_id',$data['object_id'])->where('delete_time',0)->select();
        if(!empty($ret)){
            $this->success('有东西来来来了',null,$ret);
        }else{
            $this->error('还没有人评论！快来评论吧！');
        }
        //$this->success('有东西来来来了');
    }

    /**
     *   @Notes:仅仅用于测试数据
     *   @Function test
     * @return mixed
     *   @author:
     *   @Time: 2018/11/15 0015   9:22
     * xxx
     */
    public function test(){
        $data = $this->request->param(add);
        $this->assign("test",$data);
        return $this->fetch('public@:test');
    }
}